package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.QuestDAO;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.util.HibernateUtil;

public class QuestDAOImpl implements QuestDAO {

	@Override
	public String create(Quest quest) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(quest);
		s.getTransaction().commit();
		return quest.getQuestID();
	}

	@Override
	public Quest readByID(String questID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Quest quest = (Quest)s.get(Quest.class, questID);
		s.getTransaction().commit();
		return quest;
	}

	@Override
	public void update(Quest quest) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(quest);
		s.getTransaction().commit();
	}

	@Override
	public ArrayList<Quest> searchByGame(String gameID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<Quest> qs = s.createCriteria(Quest.class).add(Restrictions.
				eq("gameID", gameID)).list();
		s.getTransaction().commit();
		return new ArrayList<Quest>(qs);
	}

	@Override
	public void delete(String questID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Quest quest = new Quest();
		quest.setQuestID(questID);
		s.delete(quest);
		s.getTransaction().commit();	

	}

}
